import tkinter as tk
from tkinter import filedialog, ttk
from merge_csv import *

class CSVApp:
    def __init__(self, root):
        self.root = root
        self.root.title("CSV Tools")
        self.root.geometry("800x600")
        
        # Main Page
        self.main_frame = tk.Frame(self.root)
        self.main_frame.pack(expand=True, fill='both')
        
        btn1 = tk.Button(self.main_frame, text="合并CSV", font=("Arial", 24), 
                        command=self.show_merge_page, width=20, height=3)
        btn1.pack(pady=20)
        
        btn2 = tk.Button(self.main_frame, text="展示图表", font=("Arial", 24),
                        command=self.show_chart_page, width=20, height=3)
        btn2.pack(pady=20)
        
        # Merge CSV Page
        self.merge_frame = tk.Frame(self.root)
        
        tk.Label(self.merge_frame, text="输入目录:", font=("Arial", 16)).pack(pady=10)
        self.input_dir = tk.Entry(self.merge_frame, width=50, font=("Arial", 14))
        self.input_dir.pack()
        tk.Button(self.merge_frame, text="选择目录", font=("Arial", 14),
                 command=self.select_input_dir).pack(pady=5)
        
        tk.Label(self.merge_frame, text="输出目录:", font=("Arial", 16)).pack(pady=10)
        self.output_dir = tk.Entry(self.merge_frame, width=50, font=("Arial", 14))
        self.output_dir.pack()
        tk.Button(self.merge_frame, text="选择目录", font=("Arial", 14),
                 command=self.select_output_dir).pack(pady=5)
        
        tk.Button(self.merge_frame, text="开始合并", font=("Arial", 18),
                 command=self.start_merge, width=20).pack(pady=30)
        
        tk.Button(self.merge_frame, text="返回", font=("Arial", 14),
                 command=self.show_main_page).pack()
        
        # Display Chart Page
        self.chart_frame = tk.Frame(self.root)
        
        left_frame = tk.Frame(self.chart_frame)
        left_frame.pack(side='left', padx=20, pady=20)
        
        tk.Label(left_frame, text="选择CSV文件:", font=("Arial", 16)).pack(pady=10)
        self.csv_file = tk.Entry(left_frame, width=50, font=("Arial", 14))
        self.csv_file.pack()
        tk.Button(left_frame, text="选择文件", font=("Arial", 14),
                 command=self.select_csv_file).pack(pady=5)
        
        tk.Button(left_frame, text="开始展示", font=("Arial", 18),
                 command=self.start_chart, width=20).pack(pady=30)
        
        tk.Button(left_frame, text="返回", font=("Arial", 14),
                 command=self.show_main_page).pack()
        
        self.right_frame = tk.Frame(self.chart_frame)
        self.right_frame.pack(side='left', padx=20, pady=20)
        
        self.chart_options = {
            # '折线图': tk.BooleanVar(),
            # '柱状图': tk.BooleanVar(),
            # '饼图': tk.BooleanVar()
        }
        
        
        self.show_main_page()
    
    def show_main_page(self):
        self.hide_all_frames()
        self.main_frame.pack(expand=True, fill='both')
    
    def show_merge_page(self):
        self.hide_all_frames()
        self.merge_frame.pack(expand=True, fill='both')
    
    def show_chart_page(self):
        self.hide_all_frames()
        self.chart_frame.pack(expand=True, fill='both')
    
    def hide_all_frames(self):
        for frame in [self.main_frame, self.merge_frame, self.chart_frame]:
            frame.pack_forget()
    
    def select_input_dir(self):
        directory = filedialog.askdirectory()
        if directory:
            self.input_dir.delete(0, tk.END)
            self.input_dir.insert(0, directory)
    
    def select_output_dir(self):
        directory = filedialog.askdirectory()
        if directory:
            self.output_dir.delete(0, tk.END)
            self.output_dir.insert(0, directory)
    
    def select_csv_file(self):
        file_path = filedialog.askopenfilename(filetypes=[("CSV files", "*.csv")])
        if file_path:
            self.csv_file.delete(0, tk.END)
            self.csv_file.insert(0, file_path)
        head_list = get_T(file_path)
        self.chart_options = {f"{head}": tk.BooleanVar(value=True) for head in head_list}
        for option, var in self.chart_options.items():
            tk.Checkbutton(self.right_frame, text=option, font=("Arial", 14),variable=var).pack(anchor='w')
        
    
    def start_merge(self):
        input_dir = self.input_dir.get()
        output_dir = self.output_dir.get()
        try:
            merge_by_hz(input_dir, output_dir)
            tk.messagebox.showinfo("合并完成", f"CSV文件已成功合并到:\n{output_dir}")
        except Exception as e:
            tk.messagebox.showerror("合并错误", f"合并过程中出现错误:\n{str(e)}")
    
    def start_chart(self):
        file_path = self.csv_file.get()
        selected_charts = [chart for chart, var in self.chart_options.items() if var.get()]
        draw_hz_csv(file_path, selected_charts)
        info(f"Displaying charts for {file_path}: {selected_charts}")

if __name__ == "__main__":
    root = tk.Tk()
    app = CSVApp(root)
    root.mainloop()
